require.config(r_config);
require(['jquery','mylayer','common','lazyload','blocksit','post_item'], function ($,mylayer,md_common,lazyload,blocksit,post_item){
	var md_post={
		init:function(){
			var _this=this;
			this.listEvent();
			//滚动加载数据
			$(window).on("scroll",function(){
				$(".water_box").each(function(){
					var list_loading=$(this).find(".list_loading");
					if(list_loading.size()==0){
						return false;
					}
					var page=parseInt(list_loading.attr("page"))?parseInt(list_loading.attr("page")):1;
					var pageCount=parseInt(list_loading.attr("pageCount"))?list_loading.attr("pageCount"):1;
					if(page>=pageCount||pageCount<=1){
						return false;
					}
					var top=$(this).offset().top;
					var height=$(this).height();
					if(top+height<=md_common.getScrollTop()+md_common.getWindowHeight()){
						var isLoad=list_loading.hasClass("loadlist")?true:false;
						if(!isLoad){
							var water_container=$(this).find(".water_container");
							list_loading.addClass('loadlist');
							list_loading.show();
							var url=list_loading.attr("url");
							var data={'page':page+1};
							var sort=$.trim($(".sort_op").val());
							if(sort){
								data.sort=sort;
							}
					        $.postAjax(url,data,function(result){
					        	list_loading.hide();
				        		if(result.list){
				        			var html=_this.getListInfo(result.list);
				        			water_container.append(html);
									//图片延迟加载
									$(".water_container .lazy").lazyload({
									    threshold : 200
									});
								}
								list_loading.attr("page",page+1).hide();
								list_loading.removeClass('loadlist');
					        });
						}
					}
				});
			});
			if($("#currentkeyword").size()>0){
				var keyword=$("#currentkeyword").val();
				$.postAjax('/search/keyword',{'keyword':keyword},function(result){});
			}
			if($(".recommend-slide").size()>0){
				$(".recommend-slide").find(".lazy").attr("src",function(){
					return $(this).attr("data-original");
				})
				require(['jcarousellite'],function(){
					$(".recommend-slide").jCarouselLite({
			            btnNext: ".n_next",
			            btnPrev: ".n_prev",
			            vertical: false,
			            speed:400,
			            visible:4,
			            scroll:4,
			            auto:5000  
		        	});
				});
			}
			$(".sort_op").on("change",function(){
				var lay=mylayer.showLoad();
				var sort=$.trim($(this).val());
				var keyword=$.trim($("#currentkeyword").val());
				var url="/search?keyword="+keyword;
				var data={};
				var site_url=url;
				if(sort){
					data.sort=sort;
					site_url=url+"&sort="+sort;
				}
				$.postAjax(url,data,function(result){
					mylayer.closeLayer(lay);
	        		if(result.list){
	        			$(".post_water").empty("");
	        			var html=_this.getListInfo(result.list);
	        			$(".post_water").html(html);
						//图片延迟加载
						$(".post_water .lazy").lazyload({
						    threshold : 200
						});
						$(".list_loading").attr("page",1).hide();
					}
				});
				history.pushState({ title: document.title,url:site_url}, document.title,site_url);
			});
		},
		getListInfo:function(data){
			var html="";
			for(var i=0;i<data.length;i++){
				if($("#share_list_"+data[i]['id']).size()==0){
					html+='<div class="grid share_list" id="share_list_'+data[i]['id']+'" data-id="'+data[i]['id']+'">'+
					'<div class="img">'+
					'<a href="javascript:void(0)" class="showDetail" data-id="'+data[i]['id']+'"><img data-id="'+data[i]['id']+'" class="showDetail lazy" data-original="'+data[i]['image']+'" /></a>'+
					'</div>'+
					'<div class="title clearfix">'+
					'<a href="javascript:void(0)" title="'+data[i]['title']+'" class="title_info showDetail left" data-id="'+data[i]['id']+'">'+data[i]['title']+'</a>'+
					'</div>'+
					'<div class="collbtn clearfix">'+
					'<div class="collbtn_info">'+
			        '<a class="right post_wish" href="javascript:void(0)" data-id="'+data[i]['id']+'">'+
			          ' <span class="icon icon-wish wish"></span><span class="text">'+data[i]['wish']+'</span>'+
			        '</a></div></div><div class="list_btn clearfix">'+
			        '<a class="left clearfix interact" href="javascript:void(0)" t="praiseup" data-id="'+data[i]['id']+'">'+
				        '<span class="icon icon-up"></span><span class="text">'+data[i]['praiseup']+'</span>'+
				    '</a>'+
				    '<a class="showDetail left clearfix" href="javascript:void(0)" data-id="'+data[i]['id']+'">'+
			    		'<span class="icon icon-view"></span><span class="text">'+data[i]['view']+'</span>'+
					'</a>'+
				    '</div></div>';
				}
			}
			return html;
		},
		listEvent:function(){
			var _this=this;
			//图片延迟加载
			$(".lazy").lazyload({
			    threshold : 200
			});
			$(".water_container").on("click",function(event){
				var e=event || window.event;
           		var elem=e.target||e.srcElement; 
           		var parent=$(elem).parent();
           		if($(elem).hasClass("showDetail")){
           			post_item.showDetail.call(elem);//显示分享详情
           		}
           		else if($(parent).hasClass("showDetail")){
           			post_item.showDetail.call(parent);//显示分享详情
           		}
           		else if($(elem).hasClass("interact")){
           			post_item.interact.call(elem);//点赞、倒彩
           		}
           		else if($(parent).hasClass("interact")){
           			post_item.interact.call(parent);//点赞、倒彩	
           		}
           		else if($(elem).hasClass("post_wish")){
           			post_item.wish.call(elem);//收藏
           		}
           		else if($(parent).hasClass("post_wish")){
           			post_item.wish.call(parent);//收藏
           		}
			});
		}
	}
	$(function(){
		md_post.init();
	});
});
